<template>
	<view class="container">
		<u-navbar
			:is-fixed="false"
			:border-bottom="false"
			:is-back="false"
			title="订单管理"
			:background="{ background: 'linear-gradient(90deg, #69b0ff, #5f88ff)' }"
			title-color="#fff"
		>
			<image slot="right" src="/static/images/home/message.png" class="message-icon" mode="widthFix"></image>
		</u-navbar>

		<view class="tab">
			<u-tabs
				:list="list"
				:is-scroll="false"
				:current="current"
				active-color="#5f88ff"
				inactive-color="#595959"
				height="100"
				@change="change"
			></u-tabs>
		</view>

		<!-- 未出行订单 -->
		<view v-if="current === 0">
			<view class="each" v-for="(item, index) in orderList" :key="index">
				<view class="date">发车日期：{{ item.ticketTime }}</view>

				<view @click="toDetail(card)" class="card" v-for="(card, cIndex) in item.allData" :key="cIndex">
					<view class="head">{{ card.startStation }}~{{ card.endStation }}</view>
					<view class="row">订单号：{{ card.no }}</view>
					<view class="row">发车时间：{{ card.ticketTime }} {{ card.startTime }} 开</view>
					<view class="row">下单时间：{{ card.orderTime }}</view>
					<view class="row">总张数：{{ card.passangers.length }} 张 
						<text class="name" v-for="(p, pIndex) in card.passangers">{{p.realname}}</text>
					</view>
					<view class="status">{{ card.status }}</view>
				</view>
			</view>
		</view>
		<view v-if="current === 1">
			<view class="each" v-for="(item, index) in orderList" :key="index">
				<view class="card" v-for="(card, cIndex) in item.allData" :key="cIndex">
					<view class="head">{{ card.startStation }}~{{ card.endStation }}</view>
					<view class="row">发车时间：{{ card.ticketTime }}{{ card.startTime }}开</view>
					<view class="row">下单时间：{{ card.orderTime }}</view>
					<view class="row">总张数：{{ card.passangers.length }}张
						<text class="name" v-for="(p, pIndex) in card.passangers">{{p.realname}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import moment from 'moment'
import { getOrder } from '../../api/system/api'
export default {
	data() {
		return {
			current: 0,
			list: [
				{
					name: '未出行订单'
				},
				{
					name: '历史订单'
				},
			],
			userInfo: {},
			orderList: []
		}
	},
	// 监听订单修改 然后刷新数据
  onShow() {
    uni.$off('refreshOrder')
    uni.$once('refreshOrder', (data) => {
      this.refreshData()
    })
  },
	// 底部切换菜单需手动更新数据
	onTabItemTap() {
		uni.getStorage({     
			key:'userInfo',
			success: (res) => {
				this.userInfo = res.data
				this.refreshData()
			}
		})
	},
	created() {
		// 获取当前用户的缓存 然后查询当前用户的订单
		uni.getStorage({     
			key:'userInfo',
			success: (res) => {
				console.log(res)
				this.userInfo = res.data
				this.refreshData()
			}
		})
	},
	methods: {
		// 跳转至订单详情并把订单数据传递过去
		toDetail(row) {
			console.log(row)
			this.$u.route('/pages/manage/detail', {
        data: JSON.stringify(row)
			})
		},
		// 获取订单信息
		refreshData() {
			getOrder({
				page: 1,
				pageSize: 1000,
				userId: this.userInfo.id
			}).then(res => {
				let orderList = []
				res.data.data.forEach((v, i) => {
					res.data.data[i].passangers = JSON.parse(res.data.data[i].passangers)
				})
				if (!this.current) {
					// 未出行订单
					orderList = res.data.data.filter(v => moment().valueOf() < moment(`${v.ticketTime} ${v.startTime}`))
				} else {
					// 历史订单
					orderList = res.data.data.filter(v => moment().valueOf() >= moment(`${v.ticketTime} ${v.startTime}`))
				}
				this.orderList = this.dealWithData(orderList)
				// 顺序排序
				this.orderList.sort((a, b) => {
					return moment(a.ticketTime).valueOf() - moment(b.ticketTime).valueOf()
				})
			})
		},
		// 切换订单类型
		change(index) {
			this.current = index
			this.refreshData()
		},
		// 处理数据 将接口返回的列表 根据订单时间进行归类处理   几号 订单是哪些
		dealWithData(data) {
			let c = [];
			let d = {};
			data.forEach(element => {
					if(!d[element.ticketTime]){
							c.push({
									ticketTime: element.ticketTime,
									allData: [element]
							});
							d[element.ticketTime] = element;
					}else{
							c.forEach(ele => {
									if(ele.ticketTime == element.ticketTime){
											ele.allData.push(element);
									}
							});
					}
										
			});
			return c;
		}
	}
}
</script>

<style lang="scss">
.swiper-box {
	height: 100vh;
}

.message-icon {
	width: 32rpx;
	height: auto;
	margin-right: 27rpx;
}

.tab {
	margin-bottom: 10rpx;
}

.gitem {
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;

	.img {
		width: 54rpx;
		height: auto;
		margin-bottom: 26rpx;
	}

	font-size: 28rpx;
	font-family: PingFang SC;
	font-weight: bold;
	color: #030305;
	line-height: 36rpx;
}

.each {
	padding: 0 30rpx;
	.date {
		color: orange;
		line-height: 80rpx;
		font-weight: bold;
		padding: 0 30rpx;
	}
	.card {
		margin-bottom: 30rpx;
		background: #e3f9f6;
		padding: 30rpx;
		border-radius: 20rpx;
		position: relative;

		.name {
			margin-left: 10rpx;
		}

		.head {
			margin-left: -15rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.row {
			line-height: 50rpx;
		}
	}

	.status {
		position: absolute;
		right: 20rpx;
		top: 40rpx;
	}
}
</style>
